package shuati.tingche;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Che {

    public static void main(String[] args) {
       Che che = new Che();
       che.tingChe("2024-02-20 10:20:30:324");

    }

//    时间戳  long     停车时间    进入时间是别人提供  <24小时  小时:分钟:秒     >24小时   天:小时:分钟:秒
//     离开时间为时间戳
     void tingChe(String oldTime){
         try {
            //    获取当前时间
             Date now = new Date();
             long nowTime = now.getTime();
             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
             Date parsedDate = dateFormat.parse(oldTime);
            //   停车时间转换成时间戳
             long timestamp = parsedDate.getTime();
             long time = nowTime-timestamp;    //算出停车时间
             // 计算停车时间中的小时、分钟、秒和毫秒
             long diffHours = time / (60 * 60 * 1000);
             long diffMinutes = (time / (60 * 1000)) % 60;
             long diffSeconds = (time / 1000) % 60;
             long diffMillis = time % 1000;
//             停车时间<24小时
             if (diffHours<24){
                 int feiYong = (int) diffHours * 5;
                 System.out.println(dateFormat.format(now));
                 System.out.println("您的停车时间是: " + diffHours + "时 : " + diffMinutes + "分: " + diffSeconds +"秒: " + diffMillis+ "毫秒");
                 System.out.println("您的停车费: " + feiYong );
             }else if(diffHours>24) {
                 long ri = diffHours/24;
                 diffHours = diffHours%24;
                 int feiYong = (int) diffHours * 3 + (int)ri*50;
                 System.out.println(dateFormat.format(nowTime));
                 System.out.println("您的停车时间是: " + ri + " 天: " + diffHours + "时 : " + diffMinutes + "分: " + diffSeconds + "秒: " + diffMillis+ "毫秒");
                 System.out.println("您的停车费: " + feiYong );
             }
         } catch (ParseException e) {
             throw new RuntimeException(e);
         }
     }
}
